package vn.edu.saigontech.ncms.web.offering;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import vn.edu.saigontech.ncms.model.info.Offering;
import vn.edu.saigontech.ncms.model.info.Teacher;
import vn.edu.saigontech.ncms.service.OfferingMemberService;
import vn.edu.saigontech.ncms.service.OfferingService;
import vn.edu.saigontech.ncms.util.CollectionUtil;
import vn.edu.saigontech.ncms.util.DateUtil;
import vn.edu.saigontech.ncms.web.BaseAction;
import vn.edu.saigontech.ncms.web.offering.report.MonthlyOfferingReport;

@SuppressWarnings("serial")
public class OfferingReportAction extends BaseAction {
	private Long offeringId;
	
	private int month;
	private int year;
	
	private String fromDate;
	private String toDate;
	
	public String showWeeklyReport() {
		REQUEST().setAttribute("fromDate", DateUtil.getDateOfThisWeek(Calendar.MONDAY));
		REQUEST().setAttribute("toDate", DateUtil.getDateOfThisWeek(Calendar.SATURDAY));
		REQUEST().setAttribute("type", "weekly");
		return SUCCESS;
	}
	
	public String doWeeklyReport() {
		Date fromDateDate = DateUtil.fromString(this.fromDate, this.i18n("format.date"));
		Date toDateDate = DateUtil.fromString(this.toDate, this.i18n("format.date")); 		
		REQUEST().setAttribute("offerings", OfferingService.INSTANCE.findByDate(fromDateDate, toDateDate));
		REQUEST().setAttribute("dates", DateUtil.sequence(fromDateDate, toDateDate));
		
		this.addLoggingDetail(BaseAction.REPORT, "logging.offering.weeklyReport");
		return SUCCESS;
	}
	
	public String showMonthlyReport() {
		REQUEST().setAttribute("type", "monthly");
		return SUCCESS;
	}
	
	public String doMonthlyReport() {
		REQUEST().setAttribute("offerings", OfferingService.INSTANCE.findByMonthAndYear(this.month - 1, this.year));
		this.addLoggingDetail(BaseAction.REPORT, "logging.offering.monthlyReport");
		return SUCCESS;
	}
	
	public String doMonthlyReport1() {
		REQUEST().setAttribute("report", new MonthlyOfferingReport(this.month, this.year));
		this.addLoggingDetail(BaseAction.REPORT, "logging.offering.monthlyReport");
		return SUCCESS;
	}
	
	public String reportEvaluation() {
		Offering offering = OfferingService.INSTANCE.findById(this.offeringId);
		REQUEST().setAttribute("offering", offering);
		REQUEST().setAttribute("offeringMembers", OfferingMemberService.INSTANCE.findByOfferingId(this.offeringId));
		
		return SUCCESS;
	}
	
	public String printTeachers(Collection<Teacher> teachers) {
		List<String> result = new ArrayList<String>();
		for (Teacher teacher : teachers) {
			result.add(this.i18n(teacher.getTitle().getKey()) + " "
					+ teacher.getFirstName());
		}
		return CollectionUtil.toString(result, " & ");
	}
	
	public int getMonth() {
		return month;
	}

	public void setMonth(int month) {
		this.month = month;
	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public String getFromDate() {
		return fromDate;
	}

	public void setFromDate(String fromDate) {
		this.fromDate = fromDate;
	}

	public String getToDate() {
		return toDate;
	}

	public void setToDate(String toDate) {
		this.toDate = toDate;
	}

	public Long getOfferingId() {
		return offeringId;
	}

	public void setOfferingId(Long offeringId) {
		this.offeringId = offeringId;
	}
}
